{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取猎头与候选人电话通话记录中的问答列表\n",
    "import os\n",
    "import sys\n",
    "import json\n",
    "import numpy as np\n",
    "from collections import Counter\n",
    "from glob import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "infile1 = './result/00004b09-509f-4e67-9be9-c0012e32ab8f_1.json'\n",
    "infile2 = './result/00004b09-509f-4e67-9be9-c0012e32ab8f_2.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# '''\n",
    "# {\n",
    "#     'action':;\n",
    "#     'code':;\n",
    "#     'data':;\n",
    "#     'sid':;\n",
    "#     'desc':;\n",
    "# }\n",
    "\n",
    "\n",
    "# 'data': {\"cn\":{\"st\":{\"bg\":\"10\",\"ed\":\"460\",\"rt\":[{\"ws\":[{\"cw\":[{\"w\":\"啊\",\"wp\":\"s\"}],\"wb\":33,\"we\":35}]}],\"type\":\"0\"}},\"seg_id\":0}\n",
    "\n",
    "# '''\n",
    "\n",
    "# speaker1_list = []\n",
    "# speaker2_list = []\n",
    "# with open(infile1, 'r', encoding='utf8') as f:\n",
    "#     j_list = json.load(f)\n",
    "#     for line in j_list:\n",
    "#         inf = json.loads(line['data'])\n",
    "# #         print(inf['cn']['st'])\n",
    "# #         print(inf['cn']['st']['type'])\n",
    "# #         print(inf['cn']['st']['rt'])\n",
    "# #         print(inf['cn']['st']['bg'])\n",
    "# #         print(inf['cn']['st']['ed'])\n",
    "#         print(inf['cn']['st']['rt'][0]['ws'][0]['cw'])\n",
    "#         print(len(inf['cn']['st']['rt'][0]['ws'][0]['cw']))\n",
    "# #         type = int(inf['cn']['st']['type'])\n",
    "# #         if type == 1:\n",
    "# #             continue        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# speaker1_info_list = []\n",
    "# speaker2_info_list = []\n",
    "# with open(infile1, 'r', encoding='utf8') as f:\n",
    "#     j_list = json.load(f)\n",
    "#     for item in j_list:\n",
    "#         sent = json.loads(item['data'])\n",
    "#         type = sent['cn']['st']['type']\n",
    "#         if type == '1':\n",
    "#             continue\n",
    "#         sent_info = {}\n",
    "#         sent_info['bg'] = sent['cn']['st']['bg']\n",
    "#         sent_info['ed'] = sent['cn']['st']['ed']\n",
    "#         rt_list = sent['cn']['st']['rt']\n",
    "#         cont = ''\n",
    "#         for rt in rt_list:\n",
    "#             ws_list = rt['ws']\n",
    "#             for ws in ws_list:\n",
    "#                 cont += ws['cw'][0]['w']\n",
    "#         sent_info['sent'] = cont\n",
    "#         speaker1_info_list.append(sent_info)\n",
    "# with open(infile2, 'r', encoding='utf8') as f:\n",
    "#     j_list = json.load(f)\n",
    "#     for item in j_list:\n",
    "#         sent = json.loads(item['data'])\n",
    "#         type = sent['cn']['st']['type']\n",
    "#         print(type)\n",
    "#         if type == '1':\n",
    "#             continue\n",
    "#         sent_info = {}\n",
    "#         sent_info['bg'] = sent['cn']['st']['bg']\n",
    "#         sent_info['ed'] = sent['cn']['st']['ed']\n",
    "#         rt_list = sent['cn']['st']['rt']\n",
    "#         cont = ''\n",
    "#         for rt in rt_list:\n",
    "#             ws_list = rt['ws']\n",
    "#             for ws in ws_list:\n",
    "#                 cont += ws['cw'][0]['w']\n",
    "#         sent_info['sent'] = cont\n",
    "#         speaker2_info_list.append(sent_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# i = 0\n",
    "# j = 0\n",
    "# dialog_list = []\n",
    "# while i < len(speaker1_info_list) and j < len(speaker2_info_list):\n",
    "#     a = speaker1_info_list[i]\n",
    "#     b = speaker2_info_list[j]\n",
    "#     if int(a['bg']) <= int(b['bg']):\n",
    "#         dialog_list.append('a:\\t{}'.format(a['sent']))\n",
    "#         i += 1\n",
    "#     else:\n",
    "#         dialog_list.append('b:\\t{}'.format(b['sent']))\n",
    "#         j += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDialogList(infile1, infile2):\n",
    "    speaker1_info_list = []\n",
    "    speaker2_info_list = []\n",
    "    with open(infile1, 'r', encoding='utf8') as f:\n",
    "        j_list = json.load(f)\n",
    "        for item in j_list:\n",
    "            sent = json.loads(item['data'])\n",
    "            type = sent['cn']['st']['type']\n",
    "            if type == '1':\n",
    "                continue\n",
    "            sent_info = {}\n",
    "            sent_info['bg'] = sent['cn']['st']['bg']\n",
    "            sent_info['ed'] = sent['cn']['st']['ed']\n",
    "            rt_list = sent['cn']['st']['rt']\n",
    "            cont = ''\n",
    "            for rt in rt_list:\n",
    "                ws_list = rt['ws']\n",
    "                for ws in ws_list:\n",
    "                    cont += ws['cw'][0]['w']\n",
    "            sent_info['sent'] = cont\n",
    "            speaker1_info_list.append(sent_info)\n",
    "    with open(infile2, 'r', encoding='utf8') as f:\n",
    "        j_list = json.load(f)\n",
    "        for item in j_list:\n",
    "            sent = json.loads(item['data'])\n",
    "            type = sent['cn']['st']['type']\n",
    "            if type == '1':\n",
    "                continue\n",
    "            sent_info = {}\n",
    "            sent_info['bg'] = sent['cn']['st']['bg']\n",
    "            sent_info['ed'] = sent['cn']['st']['ed']\n",
    "            rt_list = sent['cn']['st']['rt']\n",
    "            cont = ''\n",
    "            for rt in rt_list:\n",
    "                ws_list = rt['ws']\n",
    "                for ws in ws_list:\n",
    "                    cont += ws['cw'][0]['w']\n",
    "            sent_info['sent'] = cont\n",
    "            speaker2_info_list.append(sent_info)\n",
    "    i = 0\n",
    "    j = 0\n",
    "    dialog_list = []\n",
    "    while i < len(speaker1_info_list) and j < len(speaker2_info_list):\n",
    "        a = speaker1_info_list[i]\n",
    "        b = speaker2_info_list[j]\n",
    "        if int(a['bg']) <= int(b['bg']):\n",
    "            dialog_list.append('a:\\t{}'.format(a['sent']))\n",
    "            i += 1\n",
    "        else:\n",
    "            dialog_list.append('b:\\t{}'.format(b['sent']))\n",
    "            j += 1\n",
    "    return dialog_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "dialog_list = []\n",
    "dialog_list = getDialogList(infile1, infile2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:\t啊\n",
      "b:\t一\n",
      "b:\t喂你好\n",
      "a:\t喂你好\n",
      "a:\t一\n",
      "b:\t嗯是曾先生吧\n",
      "a:\t唉对的\n",
      "b:\t，噢我是爱丽丝\n",
      "a:\t嗯我知道\n",
      "b:\t。唉你好，嗯现在已经回去了吗\n",
      "a:\t嗯对\n",
      "b:\t？嗯感觉怎么样啊\n",
      "a:\t现在在外面\n",
      "a:\t。嗯还好，吧就是简单的沟通了一下\n",
      "b:\t？噢H啊他问道的是一些什么样的，类似一些什么样的一个问题呢\n",
      "a:\t。呃我大概就问了一下浦东的那个以前的工作经历，啊然后说对待鸟的那个看法啊\n",
      "b:\t。嗯\n",
      "a:\t，然后就是说跟之前的工作经验，跟这个工作的话有什么相类似的地方\n",
      "b:\t嗯嗯\n",
      "b:\t就是其实差不多也是我之前跟你说的那些对吧\n",
      "a:\t？唉对，差不多就是前那些吧\n",
      "b:\t？嗯嗯那他这边的一个面试形式是什么样的？是先去面的吗\n",
      "a:\t嗯嗯就是两个人一起用的嘛\n",
      "b:\t？嗯然后然后后来又单面嘛\n",
      "a:\t，啊啊我这个有点早，该提出什么\n",
      "b:\t，啊我说群面之后就结束了吗？还是\n",
      "a:\t？嗯那我等电话通知\n",
      "b:\t？噢就是先是两个人面试的，就两个人都回去等电话通知吗\n",
      "a:\t。唉对\n",
      "b:\t？噢了解了解那你现在对这个嗯岗位是怎么看法的，呀就是还有兴趣再继续\n",
      "a:\t嗯可以的\n",
      "b:\t。噢好的好的好的嗯\n",
      "b:\t，那其实差不多到时候下周一的话应该结果就会出来了，到时候我在微信上跟你说一声好吧\n",
      "a:\t嗯好的好的\n",
      "b:\t？嗯嗯嗯那你先忙\n",
      "a:\t嗯好\n"
     ]
    }
   ],
   "source": [
    "print('\\n'.join(dialog_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "indir = './result'\n",
    "file_list = glob(os.path.join(indir, '*_1.json'))\n",
    "for file in file_list:\n",
    "    basename = os.path.basename(file).replace('_1.json', '')\n",
    "    file1 = os.path.join(indir, basename+'_1.json')\n",
    "    file2 = os.path.join(indir, basename+'_2.json')\n",
    "    if (not os.path.isfile(file1)) or (not os.path.isfile(file2)):\n",
    "        continue\n",
    "    try:\n",
    "        dialog_list = []\n",
    "        dialog_list = getDialogList(file1, file2)\n",
    "        out_file = os.path.join(indir, basename+'_mix.txt')\n",
    "        with open(out_file, 'w', encoding='utf8') as f:\n",
    "            f.write('\\n'.join(dialog_list))\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
